package android.bignerdranch.sqllist;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = findViewById(R.id.btn_1);
        btn2 = findViewById(R.id.btn_2);
        btn3 = findViewById(R.id.btn_3);
        btn4 = findViewById(R.id.btn_4);
        btn5 = findViewById(R.id.btn_5);
        btn1.setOnClickListener(new CreateListener());
        btn2.setOnClickListener(new InsertListener());
        btn3.setOnClickListener(new UpdateRecordListener());
        btn4.setOnClickListener(new QueryListener());
        btn5.setOnClickListener(new DeleteRecordListener());
    }

    class CreateListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this);
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            Log.d("myDebug", "CreateSuccess");
        }
    }

    class InsertListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", "apple");
            values.put("price", 10);
            long id = db.insert("information", null, values);
            db.close();
            Log.d("myDebug", "InsertSuccess");
        }
    }

    class UpdateRecordListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", "orange");
            db.update("information", values, "_id=?", new String[]{"1"});
            db.close();
            Log.d("myDebug", "UpdateSuccess");
        }
    }

    class QueryListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            System.out.println("aaa-------");

            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this);
            SQLiteDatabase db = dbHelper.getWritableDatabase();

            Cursor cursor = db.query("information", null, "_id=?", new
                    String[]{"1"}, null, null, null);
            boolean result = cursor.moveToNext();
            String name = cursor.getString(cursor.getColumnIndex("name"));
            cursor.close();
            db.close();
            Log.d("myDebug", "query--->" + name);
        }
    }

    class DeleteRecordListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            db.delete("information", "_id=?", new String[]{"1"});
            db.close();
            Log.d("myDebug", "DeleteSuccess");
        }
    }
}